package com.example.administrator.waitan.find.news;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.find.news.newscontent.NewsAuthorContentActivity;
import com.example.administrator.waitan.tools.VolleySingleton;

import java.util.List;

/**
 * Created by Administrator on 2016/1/18.
 */
public class NewsBaseAdapter extends BaseAdapter {
    private List<NewsComics> comicses;
    private List<NewsTopic> topics;
    private List<NewsUser> users;
    private Context context;
    private ImageLoader imageLoader;
    private ImageLoader.ImageCache imageCache;
    private LruCache<String,Bitmap> lruCache;
    private RequestQueue queue;

    public NewsBaseAdapter(List<NewsComics> comicses, Context context) {
        this.comicses = comicses;
        this.context = context;
    }


    @Override
    public int getCount() {
        return comicses.size();
    }

    @Override
    public Object getItem(int i) {
        return comicses.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        MyViewHolder myViewHolder;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_lv_news,null);
            myViewHolder = new MyViewHolder();
            myViewHolder.name = (TextView) view.findViewById(R.id.tv_item_news_bookName);
            myViewHolder.title = (TextView) view.findViewById(R.id.tv_item_news_chapter);
            myViewHolder.author_name = (TextView) view.findViewById(R.id.tv_item_news_author_name);
            myViewHolder.cover = (ImageView) view.findViewById(R.id.iv_item_news_bookCover);
            myViewHolder.author = (ImageView) view.findViewById(R.id.iv_item_news_bookAuthor);
            view.setTag(myViewHolder);
        }
        else{
            myViewHolder = (MyViewHolder) view.getTag();
        }
        myViewHolder.name.setText(comicses.get(i).getTopic().getTitle());
        myViewHolder.title.setText(comicses.get(i).getTitle());
        myViewHolder.author_name.setText(comicses.get(i).getTopic().getUser().getNickname());
        VolleySingleton.getImageLoader().get(comicses.get(i).getCover_image_url(), VolleySingleton.getImageListener(myViewHolder.cover));
        VolleySingleton.getImageLoader().get(comicses.get(i).getTopic().getUser().getAvatar_url(),VolleySingleton.getImageListener(myViewHolder.author));
        myViewHolder.author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, NewsAuthorContentActivity.class);
                intent.putExtra("nickname",comicses.get(i).getTopic().getUser().getNickname());
                intent.putExtra("avatar_url",comicses.get(i).getTopic().getUser().getAvatar_url());
                intent.putExtra("signature",comicses.get(i).getTopic().getUser().getSignature());
                intent.putExtra("profile",comicses.get(i).getTopic().getUser().getProfile());
                context.startActivity(intent);
            }
        });
        return view;
    }

    class MyViewHolder{
        TextView title,name,author_name;
        ImageView author,cover;
    }
}
